<template>
  <div class="update-birthday">
    <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" @cancel="$emit('close')" @confirm="onConfirm" />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user.js'
import dayjs from 'dayjs'
export default {
  name: 'updateBirthday',

  data() {
    return {
      minDate: new Date(1980, 0, 1),
      maxDate: new Date(2025, 10, 1),
      // 当前选择的时间
      currentDate: new Date(this.value)
    }
  },

  props: {
    // 生日时间
    value: {
      type: String,
      required: true
    }
  },

  methods: {
    // 修改生日时间操作
    async onConfirm(value) {
      const changeDate = dayjs(this.currentDate).format('YYYY-MM-DD')
      await updateUserProfile({ birthday: this.changeDate })
      this.$toast.success('修改成功')
      this.$emit('close')
      // 通知父组件修改昵称
      this.$emit('input', changeDate)
    }
  }
}
</script>

<style lang="less" scope>
</style>
